package com.zhouyibike.uu.ui.activity.mybike;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.zhouyibike.uu.R;
import com.zhouyibike.uu.config.Config;
import com.zhouyibike.uu.entity.GetCodeResult;
import com.zhouyibike.uu.http.ApiCallback;
import com.zhouyibike.uu.ui.activity.web.TotalWebActivity;
import com.zhouyibike.uu.ui.adapter.MydanchevpAdapter;
import com.zhouyibike.uu.ui.base.BaseActivity;
import com.zhouyibike.uu.ui.view.MycansetscrollViewPager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;
import info.hoang8f.android.segmented.SegmentedGroup;

public class MybikeActivity6 extends BaseActivity implements View.OnClickListener {
    private ImageButton mBtnBack;
    private TextView  mBtnTitle,view_titlebar_line;
    private LinearLayout bglybglybgly;
    private MycansetscrollViewPager danchevp;
    private List<Fragment> mFragment = new ArrayList<Fragment>();
    public MyBikeinfo1Fragment f1;
    public MyBikejiaoyi1Fragment f2;
    private MydanchevpAdapter adapter;
    private SegmentedGroup gp;
    public double zhuanzengshouxufei=0;
    public int cansellnum=0;
    private LinearLayout ll_gengduo;
    private TextView tv_alltype_rengouxiexi,tv_alltype_zhuanzengxieyi,tv_alltype_zhuanzengshuoming;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mybike6);
        EventBus.getDefault().register(this);
        initview();
        initdata();
        initlistener();
    }

    private void initlistener() {
        gp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i==R.id.button21){
                    danchevp.setCurrentItem(0);
                }else if (i==R.id.button22)
                {
                    danchevp.setCurrentItem(1);
                }
            }
        });
        ll_gengduo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                backgroundAlpha(0.6f);
                showpop(view);
            }
        });
    }

    private void initdata() {
        f1=new MyBikeinfo1Fragment();
        f2=new MyBikejiaoyi1Fragment();
        mFragment.add(f1);
        mFragment.add(f2);
        adapter=new MydanchevpAdapter(getSupportFragmentManager(),mFragment);
        danchevp.setAdapter(adapter);
    }

    private void initview() {
        ll_gengduo= (LinearLayout) findViewById(R.id.ll_gengduo);
        mBtnBack = (ImageButton) findViewById(R.id.bar_btn_back);
//        mBtnBack.setBackground(getResources().getDrawable(R.drawable.fanhui2));
        mBtnTitle = (TextView) findViewById(R.id.bar_text_title);
        mBtnTitle.setText("");
//        mBtnTitle.setTextColor(getResources().getColor(R.color.colorAccent));
        bglybglybgly = (LinearLayout) findViewById(R.id.bgly);
//        bglybglybgly.setBackgroundColor(getResources().getColor(R.color.colorAccent));
        view_titlebar_line = (TextView) findViewById(R.id.view_titlebar_line);
//        view_titlebar_line.setVisibility(View.GONE);
        view_titlebar_line.setBackgroundColor(getResources().getColor(R.color.bg_main_white));
        danchevp= (MycansetscrollViewPager) findViewById(R.id.danchevp);
        danchevp.setScrollble(false);
        gp= (SegmentedGroup) findViewById(R.id.segmented2);
    }
    @Subscribe(threadMode = ThreadMode.MainThread)
    public void addbike(String result) {
        if (result.equals("buybikeok")) {
           f1.getMsg();f2.datafalsh();
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        try {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode==666&&resultCode==1)
            {
                f1.getMsg();f2.datafalsh();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    PopupWindow popupWindow;
    private void showpop(View view){
        View popupWindowView = getLayoutInflater().inflate(R.layout.pop_xieyishuoming, null);
        popupWindow = new PopupWindow(popupWindowView, WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT);
        popupWindow.setFocusable(true);
        tv_alltype_rengouxiexi=popupWindowView.findViewById(R.id.tv_alltype_rengouxiexi);
        tv_alltype_zhuanzengxieyi=popupWindowView.findViewById(R.id.tv_alltype_zhuanzengxieyi);
        tv_alltype_zhuanzengshuoming=popupWindowView.findViewById(R.id.tv_alltype_zhuanzengjiaocheng);
        tv_alltype_rengouxiexi.setOnClickListener(this);
        tv_alltype_zhuanzengxieyi.setOnClickListener(this);
        tv_alltype_zhuanzengshuoming.setOnClickListener(this);
        // 设置允许在外点击消失
        popupWindow.setOutsideTouchable(true);
        // 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        int width = metric.widthPixels;     // 屏幕宽度（像素）
        int pos = width - popupWindow.getWidth();
        popupWindow.showAsDropDown(this.findViewById(R.id.view_titlebar_line), pos, 0);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
            }
        });
    }
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        getWindow().setAttributes(lp);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_alltype_rengouxiexi:
                popupWindow.dismiss();
                getWZMsg("5","单车认购协议");
                break;
            case R.id.tv_alltype_zhuanzengxieyi:
                popupWindow.dismiss();
                getWZMsg("10","单车转赠协议");
                break;
            case R.id.tv_alltype_zhuanzengjiaocheng:
                popupWindow.dismiss();
                Intent intent3 = new Intent(MybikeActivity6.this, FirstZhuanzengActivity.class);
                intent3.putExtra("cansellnum", cansellnum);
                intent3.putExtra("zhuanzengprice", zhuanzengshouxufei);
                startActivityForResult(intent3, 666);
                break;}
    }
    private void getWZMsg(String type, final String name) {
        Map<String, String> params = new HashMap<>();
        params.put("token", preferences.getString("token", ""));
        params.put("type", type);
        showProgressDialog();
        addSubscription(apiStores.GetInformationzcxy(params),
                new ApiCallback<GetCodeResult>() {

                    @Override
                    public void onSuccess(GetCodeResult model) {
                        if (model.getStatus() == Config.SUCCESSCODE) {
                            Intent i = new Intent(MybikeActivity6.this, TotalWebActivity.class);
                            i.putExtra("title", name);
                            i.putExtra("url", model.getData());
                            startActivity(i);
                        } else {
                            toastShow(model.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        toastShow(msg);
                    }

                    @Override
                    public void onFinish() {
                        dismissProgressDialog();
                    }
                });
    }
}
